;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname u1-huebung) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
;; 9.1
;; contract:    convert-to-nfeet: number symbol -> number
;; description: Converts one distance to Nubischer Königsfuß.
;; example:     (convert-to-nfeet 10 'meter) -> ~ 37.7929

(define (convert-to-nfeet distance unit)
  (cond
    [(symbol=? unit 'nfeet)      distance]
    [(symbol=? unit 'meter)   (/ distance 0.2646)]
    [(symbol=? unit 'faden)   (/ distance 0.1447)]
    [(symbol=? unit 'zoll)    (/ distance 10.4173)]
    [(symbol=? unit 'klafter) (/ distance 0.1407)]
    [(symbol=? unit 'yard)    (/ distance 0.2894)]
    [else
        (error 'convert-to-nfeet "No valid unit given")]))

;; Tests
(check-within (convert-to-nfeet 23.5 'zoll)   2.25586     0.00001)
(check-within (convert-to-nfeet 1337 'faden)  9239.806496 0.00001)
(check-error  (convert-to-nfeet 1234 'cookie) "convert-to-nfeet: No valid unit given")



;; 9.2
;; contract:    convert-from-nfeet: number symbol -> number
;; description: Converts Nubischer Königsfuß to any distance.
;; example:     (convert-from-nfeet 1 'yard) -> 0.2894

(define (convert-from-nfeet distance unit)
         (/ (sqr distance) (convert-to-nfeet distance unit))) ; = convert-from-nfeet = distance (distance / convert-to-nfeet)

;; Tests
(check-within (convert-from-nfeet 15 'klafter) 2.1105 0.00001)
(check-within (convert-from-nfeet 5  'yard)    1.447  0.00001)



;; 9.3
;; contract:    convert-x-to-y: number symbol symbol -> number
;; description: Converts one distance to Nubischer Königsfuß and back to another.
;; example:     (convert-x-to-y 17.3 'meter 'zoll) -> 681.1008692

(define (convert-x-to-y distance base-unit target-unit)
  (cond
    
    [(and (symbol=? base-unit 'meter) (symbol=? target-unit 'meter))
     (* 0.98 (convert-from-nfeet (* 0.98 (convert-to-nfeet distance base-unit)) target-unit))]
    
    [(or (symbol=? base-unit 'meter) (symbol=? target-unit 'meter))
     (* 0.98 (convert-from-nfeet (convert-to-nfeet distance base-unit) target-unit))]
    
    [else
     (convert-from-nfeet (convert-to-nfeet distance base-unit) target-unit)]))

;; Tests
(check-within (convert-x-to-y 3.15 'meter    'meter)  3.02526     0.00001)
(check-within (convert-x-to-y 57   'meter    'zoll)   2199.207778 0.00001)
(check-within (convert-x-to-y 7.3  'faden    'meter)  13.08188252 0.00001)
(check-within (convert-x-to-y 12.5 'klafter  'yard)   25.71073205 0.00001)
(check-error  (convert-x-to-y 1234 'distance 'cookie) "convert-to-nfeet: No valid unit given")